package com.hang.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hang.common.BizErrorCodeEnum;
import com.hang.common.BizException;
import com.hang.mapper.ClazzMapper;
import com.hang.model.Clazz;
import com.hang.model.Teacher;
import com.hang.service.TeacherService;
import com.hang.mapper.TeacherMapper;
import com.hang.util.JwtUtil;
import com.hang.util.listener.TeacherListener;
import com.hang.vo.LoginVo;
import com.hang.vo.TeacherVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
@Slf4j
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 导入教师信息
     * @param multipartFile
     */
    @Override
    public void insertInformation(MultipartFile multipartFile) {
        try{
            EasyExcel.read(multipartFile.getInputStream(), TeacherVo.class,new TeacherListener(baseMapper))
                    .sheet()
                    .doRead();
        }catch (IOException e){
            log.error("导入信息失败:{}",e.toString());
        }
    }

    /**
     * 获取班级列表（指定老师）
     * @param teacherId
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<Clazz> getHisList(String teacherId, Integer page, Integer size) {
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);

        Page<Clazz> pages = new Page<>(page,size);
        IPage<Clazz> iPage = clazzMapper.selectPage(pages,queryWrapper);

        List<Clazz> records = iPage.getRecords();

        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("teacher_id",teacherId);

        Teacher teacher = baseMapper.selectOne(teacherQueryWrapper);

        String teacherName = null;
        if(!ObjectUtils.isEmpty(teacher)){
           teacherName = teacher.getRealName();
        }

        for (Clazz record : records) {
            record.setTeacherName(teacherName);
        }

        return records;
    }

    @Override
    public Boolean hasMoreData(String teacherId,Integer page, Integer size) {
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);

        int totals = clazzMapper.selectCount(queryWrapper);
        return totals>page*size ? Boolean.TRUE:Boolean.FALSE;
    }

    @Override
    public Map<String, Object> login(LoginVo loginVo) {
        if(ObjectUtils.isEmpty(loginVo)){
            throw new BizException(BizErrorCodeEnum.OBJECT_IS_NULL);
        }

        if(StringUtils.isBlank(loginVo.getLoginId())||StringUtils.isBlank(loginVo.getPassword())){
            throw new BizException(BizErrorCodeEnum.PARAM_IS_NULL);
        }

        //先去老师的数据库中进行用户密码校验
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",loginVo.getLoginId());
        queryWrapper.eq("password",loginVo.getPassword());

        Teacher teacher = baseMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(teacher)){
            throw new BizException(BizErrorCodeEnum.LOGIN_FAIL);
        }

        //校验登录成功
        Map<String,Object> map = new HashMap<>();
        map.put("teacherId",teacher.getTeacherId());
        map.put("realName",teacher.getRealName());

//        String token = JwtHelper.createTeacherToken(teacher.getTeacherId());
        String token = JwtUtil.createTeacherToken(teacher.getTeacherId());

        map.put("token",token);

        return map;
    }
}